//
// Copyright (c) 2010 Luís Reis
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
using System;

namespace Pine.Lexer
{
	public enum TokenType
	{
		Invalid,
		Null,
		Word,
		Keyword,
		Number,
		Symbol,
		CharConstant,
		StringConstant,
		End
	}
	
	public enum Symbol
	{
		LogicalNot,
		Modulo,
		BitwiseAnd,
		BitwiseOr,
		BitwiseXor,
		Divide,
		LeftParenthesis,
		RightParenthesis,
		LeftBlock,
		RightBlock,
		Equal,
		LeftArray,
		RightArray,
		Conditional,
		LessThan,
		GreaterThan,
		Plus,
		Minus,
		Times,
		BitwiseNot,
		Dot,
		Colon,
		Comma,
		Semicolon,
		LogicalAnd,
		LogicalOr,
		Increment,
		Decrement,
		LeftShift,
		RightShift,
		Equals,
		NotEqual,
		LessOrEqual,
		GreaterOrEqual,
		PlusEqual,
		MinusEqual,
		TimesEqual,
		DivideEqual,
		Range,
		ArrayKeyValue,
		ModuloEqual,
		RightShiftEqual,
		LeftShiftEqual,
		Ellipsis
	}
	
	public sealed class Token
	{
		public TokenType Type { get; set; }
		public int Line { get; set; }
		public int Col { get; set; }
		
		public object Content { get; set; }
		
		public Token ()	: this(0, 0, TokenType.Null, null) {}
		
		public Token(TokenType type) : this(0, 0, type, null) {}
		
		public Token(int line, int col) : this(line, col, TokenType.Null, null) {}
		
		public Token(int line, int col, TokenType type) : this(line, col, type, null) {}
		
		public Token(int line, int col, TokenType type, object content) {
			Line = line;
			Col = col;
			Type = type;
			Content = content;
		}
		
		public static Token FromDouble(int line, int col, double number) {
			return new Token(line, col, TokenType.Number, number);
		}
		
		public static Token FromString(int line, int col, string str) {
			return new Token(line, col, TokenType.StringConstant, str);
		}
		
		public static Token FromChar(int line, int col, char chr) {
			return new Token(line, col, TokenType.CharConstant, chr);	
		}
		
		public static Token FromWord(int line, int col, string word) {
			return new Token(line, col, word.IsKeyword() ? TokenType.Keyword : TokenType.Word, word);	
		}
		
		public static Token FromSymbol(int line, int col, Symbol symbol) {
			return new Token(line, col, TokenType.Symbol, symbol);
		}
		
		public static Token FromError(int line, int col, string error) {
			return new Token(line, col, TokenType.Invalid, error);	
		}
		
		public bool IsKeyword(string keyword) {
			if (keyword == null)
				throw new ArgumentNullException("keyword");
			
			return Type == TokenType.Keyword && keyword.Equals(Content);
		}
		
		public bool IsSymbol(Symbol symbol) {			
			return Type == TokenType.Symbol && ((object) symbol).Equals(Content);	
		}
		
		public override string ToString ()
		{
			string cont = Content == null ? null : Content.ToString();
			if (Content is double)
				cont = ((double) Content).ToString(System.Globalization.CultureInfo.InvariantCulture);
			
			return cont;
		}
	}
}

